package com.astrofizzbizz.stoneedgegui.monitor;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.astrofizzbizz.stoneedgegui.utilities.AxisCamera2;
import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIITunnel;



public class DomeMonitorPanel extends JPanel 
{
	private static final long serialVersionUID = -9083047399651891038L;
	private JPanel lightControlPanel;
	private JPanel cameraControlPanel;
	private JPanel domeStatusPanel;
	private JPanel lightCameraControlPanel;
	private JPanel domeControlPanel;
	private AxisCamera2 axisCameraPanel;
	private int cameraWidth = 720;
	private int cameraHeight = 500;
	private DomeMonitor domeMonitor = null;
	private DomeLightControl domeLightControl;
	private JButton[] domeOpenCloseButton = new JButton[2];
	private TelescopeMonitorPanel telescopeMonitorPanel;


	public DomeMonitorPanel(TelescopeMonitorPanel telescopeMonitorPanel)
	{
        this.telescopeMonitorPanel = telescopeMonitorPanel;
        domeLightControl = new DomeLightControl(telescopeMonitorPanel);

        setupMainPanel();
	}
	public void setSize()
	{
		lightCameraControlPanel.setMaximumSize(new Dimension(100000,lightCameraControlPanel.getMinimumSize().height));
		Dimension d = new Dimension(lightCameraControlPanel.getPreferredSize().width,
				lightCameraControlPanel.getMinimumSize().height); 
		lightCameraControlPanel.setMinimumSize(d);
		lightCameraControlPanel.setPreferredSize(d);
		lightCameraControlPanel.setSize(d);
		
//		domeControlPanel.setVisible(true);
	}
	public void cleanup()
	{
    	if(domeLightControl.isLightsOn())
    	{
    		domeLightControl.lightSwitch(false);
        	try {Thread.sleep(100);} catch (InterruptedException e) {;}
    	}
    	axisCameraPanel.disconnect();
    	try {Thread.sleep(100);} catch (InterruptedException e) {;}
	    System.out.println("Camera disconnected");
	}
	private void setupMainPanel()
	{
		setupLightControlPanel();
		setupDomeStatusPanel();
		setupCameraControlPanel();
		setupDomeControlPanel();
		lightCameraControlPanel = new JPanel();
		lightCameraControlPanel.setLayout(new BoxLayout(lightCameraControlPanel, BoxLayout.X_AXIS));
		lightCameraControlPanel.add(domeStatusPanel);
		lightCameraControlPanel.add(domeControlPanel);
		lightCameraControlPanel.add(lightControlPanel);
		lightCameraControlPanel.add(cameraControlPanel);


		axisCameraPanel = new AxisCamera2("localhost", StoneEdgeIIITunnel.getDomeCameraPort(), cameraWidth, cameraHeight);
	    Thread axisCamera1Thread = new Thread(axisCameraPanel);
	    axisCamera1Thread.start();

	    while (!axisCameraPanel.isImageDrawn())
	    {
	    	try {Thread.sleep(100);} catch (InterruptedException e) {}
	    }
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
	    add(axisCameraPanel);	    
	    add(lightCameraControlPanel);
	}
	private void setupDomeStatusPanel()
	{
		domeStatusPanel = new JPanel();
		domeStatusPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dome Status"),BorderFactory.createEmptyBorder(5,5,5,5)));
		domeStatusPanel.setLayout(new BoxLayout(domeStatusPanel, BoxLayout.Y_AXIS));
		domeMonitor = new DomeMonitor(true, telescopeMonitorPanel);
		domeStatusPanel.add(domeMonitor.getTrackingOffLabel());
		domeStatusPanel.add(domeMonitor.getSlitClosedLabel());
		domeStatusPanel.add(domeMonitor.getCantOpenReasonLabel());
		domeStatusPanel.add(domeMonitor.getDomeLocationLabel());
		domeStatusPanel.add(domeMonitor.getLockNameLabel());
		domeStatusPanel.add(domeMonitor.getLockPhoneLabel());

	    Thread domeMonitorThread = new Thread(domeMonitor);
	    domeMonitorThread.start();

	}
	private void setupLightControlPanel()
	{

		JButton[] onOffButton = new JButton[2];
		onOffButton[0] = new JButton("On");
		onOffButton[1] = new JButton("Off");
		onOffButton[0].addActionListener(new DomeMonitorPanelActionListeners("Dome.Light.On", this));
		onOffButton[1].addActionListener(new DomeMonitorPanelActionListeners("Dome.Light.Off", this));

		JPanel onOffPanel = new JPanel();
		onOffPanel.setLayout(new FlowLayout());
		onOffPanel.add(onOffButton[0]);
		onOffPanel.add(onOffButton[1]);

		domeLightControl.setLightStayOnProgress( new JProgressBar(0, 60));
		
		domeLightControl.setLightStayOnProgressPanel(new JPanel());
		domeLightControl.getLightStayOnProgressPanel().setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Light On"),BorderFactory.createEmptyBorder(5,5,5,5)));
		domeLightControl.getLightStayOnProgressPanel().add(domeLightControl.getLightStayOnProgress());
		
		lightControlPanel = new JPanel();
		lightControlPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Light Control"),BorderFactory.createEmptyBorder(5,5,5,5)));
		lightControlPanel.setLayout(new BoxLayout(lightControlPanel, BoxLayout.Y_AXIS));
		lightControlPanel.add(onOffPanel);
		lightControlPanel.add(domeLightControl.getLightStayOnProgressPanel());
		
		domeLightControl.getLightStayOnProgressPanel().setVisible(false);
	    Thread domeLightControlThread = new Thread(domeLightControl);
	    domeLightControlThread.start();
		
				
		return;
	}
	private void setupDomeControlPanel()
	{
		domeControlPanel = new JPanel();

		domeOpenCloseButton[0] = new JButton("Open");
		domeOpenCloseButton[1] = new JButton("Close");
		domeOpenCloseButton[0].addActionListener(new DomeMonitorPanelActionListeners("Dome.Open", this));
		domeOpenCloseButton[1].addActionListener(new DomeMonitorPanelActionListeners("Dome.Close", this));

		domeControlPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dome Control"),BorderFactory.createEmptyBorder(5,5,5,5)));
		domeControlPanel.setLayout(new GridLayout(2,1, 5, 5));
		domeControlPanel.add(domeOpenCloseButton[0]);
		domeControlPanel.add(domeOpenCloseButton[1]);
		domeOpenCloseButton[0].setVisible(false);
		domeOpenCloseButton[1].setVisible(false);
		domeControlPanel.setVisible(false);
		return;
	}
	private void setupCameraControlPanel()
	{
		cameraControlPanel = new JPanel();
		cameraControlPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Camera Control"),BorderFactory.createEmptyBorder(5,5,5,5)));
		cameraControlPanel.setLayout(new GridLayout(3,3, 5, 5));

		JButton cameraUpButton = new JButton("Up");
		JButton cameraDownButton = new JButton("Down");
		JButton cameraLeftButton = new JButton("Left");
		JButton cameraRightButton = new JButton("Right");
		JButton cameraZoomOutButton = new JButton("Zoom Out");
		JButton cameraZoomInButton = new JButton("Zoom In");
		
		cameraUpButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.Up", this));
		cameraDownButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.Down", this));
		cameraLeftButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.Left", this));
		cameraRightButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.Right", this));
		cameraZoomOutButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.ZoomOut", this));
		cameraZoomInButton.addActionListener(new DomeMonitorPanelActionListeners("Camera.ZoomIn", this));

		cameraControlPanel.add(new JLabel("  "));
		cameraControlPanel.add(cameraUpButton);
		cameraControlPanel.add(new JLabel("  "));
		cameraControlPanel.add(cameraLeftButton);
		cameraControlPanel.add(new JLabel("  "));
		cameraControlPanel.add(cameraRightButton);
		cameraControlPanel.add(new JLabel("  "));
		cameraControlPanel.add(cameraDownButton);
		cameraControlPanel.add(new JLabel("  "));

	}
	public void openDome()
	{
		DomeControl domeControl = new DomeControl(telescopeMonitorPanel, true);
	    Thread domeControlThread = new Thread(domeControl);
	    domeControlThread.start();
//	    messageDialog("Opening Dome - Please Wait");
	}
	public void closeDome()
	{
		DomeControl domeControl = new DomeControl(telescopeMonitorPanel, false);
	    Thread domeControlThread = new Thread(domeControl);
	    domeControlThread.start();
//	    messageDialog("Closing Dome - Please Wait");
	}
	public TelescopeMonitorPanel getTelescopeMonitorPanel() {return telescopeMonitorPanel;}
	protected void messageDialog(String string)
	{
		JOptionPane.showMessageDialog(telescopeMonitorPanel.getStoneEdgeIIIGui(), string);
	}
	public int getCameraWidth() {return cameraWidth;}
	public int getCameraHeight() {return cameraHeight;}
	public DomeMonitor getDomeMonitor() {return domeMonitor;}
	public DomeLightControl getDomeLightControl() {return domeLightControl;}
	public JButton[] getDomeOpenCloseButton() {return domeOpenCloseButton;}
	public JPanel getDomeControlPanel() {return domeControlPanel;}
	public AxisCamera2 getAxisCameraPanel() {return axisCameraPanel;}


}
